package chapter3.lessons.objectOutputStreamPart;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * @Project ：learn_java
 * @File ：ObjectOutputStreamDemo01.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/3/1 19:52
 * @Description ：对象流的使用
 */

public class ObjectOutputStreamDemo01 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        /*
        ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream("G:\\codes\\learn_java\\IOTestOutputFiles\\test.txt"));

        LocalDate currentDate = LocalDate.now();

        // 获取年、月、日信息
        int year = currentDate.getYear();
        // int month = currentDate.getMonthValue();
        // int day = currentDate.getDayOfMonth();

        Student s = new Student("刘亦菲", Math.abs(year - 1987));

        oos.writeObject(s);
        oos.flush();
        oos.close();
        */

        // 即使文件里存储的是乱码，但是可以反序列化回来
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("G:\\codes\\learn_java\\IOTestOutputFiles\\test.txt"));
        Object obj = ois.readObject();
        Student s = (Student) obj;
        System.out.println(s.getName() + " " + s.getAge());
        System.out.println(s);
        ois.close();
    }
}
